<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/27
 * Time: 11:15
 */

namespace tools;

class  Server
{
    /**
     * 密钥 - 供客户端使用
     * @var array
     */
    private static $key = [
        '1111',
        '2222'
    ];

    /**
     * 检查Sign
     * @param array $params
     * @return bool
     */
    protected function checkSign($params = [])
    {
        if (empty($params['sign'])) return false;

        $sign = $params['sign'];
        ksort($params);
        $param_str = '';

        foreach ($params as $k => $v) {
            if (empty($v) || $v == $sign) continue;
            $param_str .= $k . '=' . $v . '&';
        }
        $param_str = rtrim($param_str, '&');

        foreach (self::$key as $vv) {
            if (md5($param_str . $vv) === $sign) return true;
        }

        return false;
    }

    /**
     * 接口处理响应
     * @param array $params
     * @return array
     */
    protected function response($params = [])
    {
        $response = [];
        $response['status'] = $params['status'];
        $response['code'] = $params['code'];
        $response['msg'] = $params['msg'];
        $response['data'] = $params['data'];
        return $response;
    }

}